package com.itheima.question.timer;

import com.itheima.question.common.contants.RedisKeyContstants;
import com.itheima.question.config.FastDFSClientUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Set;

/**
 * @ClassName TaskDemo
 * @Description TODO
 * @Author lh
 * @Date 2021/12/22
 **/
@Component
@Slf4j
public class ClearPicTask {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private FastDFSClientUtil fastDFSClientUtil;


    @Scheduled(cron = "0/5 0 * * * ?")
    public void clearPic() {
        Set<String> pics = redisTemplate.opsForSet().difference(RedisKeyContstants.ALL_OIC, RedisKeyContstants.USE_OIC);
        log.info("定时任务开启，需要清理的图片有"+ pics);
        if(pics!=null) {
            for (String pic: pics) {
                fastDFSClientUtil.delFile(pic);
            }
        }


    }
}
